const initialState = {
  list: [] as Channel[],
  userChannels: [] as Channel[],
  activeId: 0,
};

// 定义action类型
export type ChannelAction =
  // 首页的频道列表action
  | { type: 'channel/saveList'; payload: Channel[] }
  //   我的频道列表action
  | { type: 'channel/myChannelList'; payload: Channel[] }
  //   点击添加频道
  | { type: 'channel/addChannelList'; payload: Channel }
  //   点击频道高亮并跳转
  | { type: 'channel/changeChannelactiveId'; payload: number }
  // 点击删除图标删除频道
  | { type: 'channel/deleteChannel'; payload: number };
// 定义频道数据类型
export type Channel = {
  id: number;
  name: string;
};

export default function channelReducer(
  state = initialState,
  action: ChannelAction
): typeof initialState {
  switch (action.type) {
    //   首页频道列表
    case 'channel/saveList':
      return {
        ...state,
        list: action.payload,
      };

    //   我的频道列表
    case 'channel/myChannelList':
      return {
        ...state,
        userChannels: action.payload,
      };

    // //   点击添加频道
    // case 'channel/addChannelList':
    //   return {
    //     ...state,
    //     userChannels: [...state.userChannels, action.payload],
    //   };

    //   点击频道高亮并且跳转
    case 'channel/changeChannelactiveId':
      return {
        ...state,
        activeId: action.payload,
      };

    // 点击删除频道
    case 'channel/deleteChannel':
      return {
        ...state,
        userChannels: state.userChannels.filter(
          (item) => item.id !== action.payload
        ),
      };

    default:
      return state;
  }
}
